/*
	Twitch
	Please leave credits to the origional devs where its due if edited.
*/

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self:SetModel("models/Items/combine_rifle_ammo01.mdl")
	self:PhysicsInit(SOLID_BBOX)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then
		phys:Wake()
		phys:EnableGravity(false)
	end
	self:StartMotionController() // Activate the motion controller for ENT.PhysicsSimulate
	local Tab = GAMEMODE:GetProjectile(self:GetProjectile())
	if Tab and type(Tab.Init) == "function" then // Check if our current projectile has an Init function and call it.
		Tab.Init(self)
	end
end

function ENT:PhysicsSimulate(phys,deltatime)
	local Speed = GAMEMODE:GetProjectile(self:GetProjectile())
	if Speed and type(Speed.Speed) == "number" then // Checking if we have a set speed in the projectile so we can apply it.
		Speed = Speed.Speed
	else
		Speed = 1600
	end
	phys:Wake()
	local pr = {}
	pr.secondstoarrive	= 3
	pr.pos				= self:GetPos()+(self:GetForward()*Speed)
	pr.maxangular		= 90000
	pr.maxangulardamp	= 90000
	pr.maxspeed			= 1000000
	pr.maxspeeddamp		= 10000
	pr.dampfactor		= 0
	pr.teleportdistance	= 5000
	pr.deltatime		= deltatime
	pr.angle			= self:GetAngles()
	phys:ComputeShadowControl(pr)
end

function ENT:SetProjectile(str)
	if GAMEMODE:GetProjectile(str) then
		self:SetNWString("TWC_Projectile",str)
	end
end

function ENT:PhysicsCollide(data,physobj)
	local Func = GAMEMODE:GetProjectile(self:GetProjectile())
	if Func and type(Func.Hit) == "function" then
		return Func.Hit(self,data,physobj)
	else
		return self.DefaultHitFunction(self,data,physobj)
	end
end

function ENT:DefaultHitFunction(data,physobj)
	data.HitEntity:TakeDamage(10,self:GetOwner(),self)
	self:Remove()
	return
end
